
package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.mongodb.client.model.Filters;
import com.weiqiao.mall.domain.ShelfEntity;
import com.weiqiao.mall.repository.IShelfRepository;
import com.weiqiao.mall.service.IShelfService;
import com.weiqiao.utils.WeiXinUtils;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 微信商城  店铺装修 配置 业务层实现类，继承BaseWeiXinBizImpl，实现IShelfBiz
 *
 * @author tiyer
 */
@Service("ShelfServiceImpl")
public class ShelfServiceImpl extends BaseWeiXinServiceImpl implements IShelfService {


    /**
     * 注入产品管理持久化层
     */
    @Autowired
    @Qualifier("ShelfRepositoryImpl")
    private IShelfRepository shelfDao;


    @Override
    public ShelfEntity getShelfInf(Bson filter) {

        ShelfEntity entity = new ShelfEntity();
        String shelf = shelfDao.getOneByFilter(filter);
        if (!StringUtil.isBlank(shelf)) {
            entity = new Gson().fromJson(shelf, ShelfEntity.class);
        }
        return entity;
    }

    @Override
    public List<ShelfEntity> getShelfs(int company_id) {
        Bson filter = Filters.eq("company_id", company_id);
        List<ShelfEntity> shelf = new ArrayList<ShelfEntity>();
        List<String> list = shelfDao.getShelfs(filter);
        if (!StringUtil.isBlank(list)) {
            for (String a : list) {
                ShelfEntity s = new Gson().fromJson(a, ShelfEntity.class);
                shelf.add(s);
            }
        }
        return shelf;
    }

    /**
     * 转换时间戳为时间字符串
     */
    public String formateTime(String dateStr) {
        try {
            if (StringUtil.isLong(dateStr)) {
                String date = WeiXinUtils.format(new Date(Long.parseLong(dateStr)), "yyyy-MM-dd HH:mm");
                dateStr = date;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return dateStr;
    }

    @Override
    public boolean deleteById(int companyId, String shelf_id) {
        Bson filter1 = Filters.eq("company_id", companyId);
        Bson filter2 = Filters.eq("shelf_id", shelf_id);
        Bson filter = Filters.and(filter1, filter2);
        return shelfDao.deleteById(filter);
    }

    @Override
    public boolean updateShelf(Bson filter, String json) {
        return shelfDao.updateShelf(filter, json);
    }

    @Override
    public List<ShelfEntity> getshelfs(Bson filter) {
        List<ShelfEntity> shelf = new ArrayList<ShelfEntity>();
        List<String> list = shelfDao.getShelfs(filter);
        if (!StringUtil.isBlank(list)) {
            for (String a : list) {
                ShelfEntity s = new Gson().fromJson(a, ShelfEntity.class);
                shelf.add(s);
            }
        }
        return shelf;
    }
}
